package com.mmbang.util;

import java.io.*;

import com.mmbang.util.FileRes;
import com.mmbang.util.StringOperation;

public class WriteAppend {

	private String path;// 文件路径
	private String something;// 追加的字符串变量

	// 初始化
	public WriteAppend() {
		path = null;
		something = "Default message";
	}

	// 设置文件路径
	public void setPath(String apath) {
		path = apath;
	}

	// 得到文件路径
	public String getPath() {
		return path;
	}

	// 设置要追加的字符串
	public void setSomething(String asomething) {
		something = asomething;
	}

	// 得到要追加的字符串
	public String getSomething() {
		return something;
	}

	// 追加字符串
	public void  writeSomething() { 
		try { 
		// 创建文件path并写入something字符串，注意和写入篇的区别
		FileWriter theFile = new FileWriter(path,true); 
		PrintWriter out = new PrintWriter(theFile); 
		out.print(something + "\n"); 
		out.close(); 
		// 关闭文件并返回success字符串
		theFile.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
	
	public void writeJson() {
		FileWriter fw;
		try {
			fw = new FileWriter(path);
			PrintWriter out = new PrintWriter(fw);
			out.write(something);
			out.println();
			fw.close();
			out.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}
	
	
	

	

	//写抓取到的详细页内容
	public void writeItem(String webName,String strValue,String num) {
//		String category_id = "1376";

		String filePath = FileRes.getPropValue("filePath");
		filePath += webName;
		filePath +=File.separator + "itemdetail" + File.separator + "item_"+num+".txt";
		setPath(filePath);
		setSomething(strValue);
		writeSomething();

	}
	
	//写抓取到的详细页内容(博客)
	public void writeItem(String webName,String strValue,String num,String blogAuthorName) {
//		String category_id = "1376";

		String filePath = FileRes.getPropValue("filePath");
		filePath += webName;
		filePath +=File.separator + blogAuthorName + File.separator + "itemdetail" + File.separator + "item_"+num+".txt";
		setPath(filePath);
		setSomething(strValue);
		writeSomething();

	}
	
	
	//写抓取到的列表页内容，用于价格更新
	public void writeListItem(String webName,String strValue) {
//		String category_id = "1376";

		String filePath = FileRes.getPropValue("html.filePath");
		filePath += webName;
		filePath +=File.separator + "listitem.txt";
		setPath(filePath);
		setSomething(strValue);
		writeSomething();

	}	
	
	
	
	//写详细页的地址
	public void writeItemUrl(String webName,String strUrl) {
//		String category_id = "1376";

		String filePath = FileRes.getPropValue("filePath");
		filePath += webName;
		filePath +=File.separator + "itemurl.txt";
		setPath(filePath);
		setSomething(strUrl);
		writeSomething();
	}
	
	//写详细页的地址
	public void writeItemUrl(String webName,String strUrl,String num) {
//		String category_id = "1376";

		String filePath = FileRes.getPropValue("filePath");
		filePath += webName;
		filePath +=File.separator + "itemurl" + File.separator +  "itemurl_"+num+".txt";
		setPath(filePath);
		setSomething(strUrl);
		writeSomething();
	}
	
	//写详细页的地址(博客)
	public void writeItemUrl(String webName,String blogAuthorName,String strUrl,String num) {
//		String category_id = "1376";

		String filePath = FileRes.getPropValue("filePath");
		filePath += webName;
		filePath +=File.separator + blogAuthorName + File.separator + "itemurl" + File.separator +  "itemurl_"+num+".txt";
		setPath(filePath);
		setSomething(strUrl);
		writeSomething();
	}	
	
	//写列表页的地址
	public void writeListUrl(String webName,String strUrl) {
		String filePath = FileRes.getPropValue("filePath");
		filePath += webName;
		filePath +=File.separator + "listurl" + File.separator + "listurl.txt";
		setPath(filePath);
		setSomething(strUrl);
		writeSomething();
	}
	
	//写列表页的地址(博客)
	public void writeListUrl(String webName,String blogName,String strUrl) {
		String filePath = FileRes.getPropValue("filePath");
		filePath += webName;
		filePath +=File.separator + blogName + File.separator + "listurl" + File.separator + "listurl.txt";
		setPath(filePath);
		setSomething(strUrl);
		writeSomething();
	}
	
	
	//写xml导库数据
	public void writeXml(String webName,StringBuffer strBuf,String askyayaColumnId) {

		String filePath = FileRes.getPropValue("listurl.filePath");
		filePath += webName;
		filePath +=File.separator + "input_"+ askyayaColumnId +".xml";
		setPath(filePath);
		setSomething(strBuf.toString());
		writeSomething();
	}
	
	//写xml导库数据(根据数据量来生成数据文件)
	public void writeXml(String webName,StringBuffer strBuf,String askyayaColumnId,String fileNumber) {

		String filePath = FileRes.getPropValue("listurl.filePath");
		filePath += webName;
		filePath +=File.separator + "input_"+ askyayaColumnId +"_"+ fileNumber+".xml";
		setPath(filePath);
		setSomething(strBuf.toString());
		writeSomething();
	}
	
	//写xml导库数据_列表页处理而来，主要是为了更新价格
	public void writeListXml(String webName,StringBuffer strBuf,String askyayaColumnId) {

		String filePath = FileRes.getPropValue("listurl.filePath");
		filePath += webName;
		filePath +=File.separator + "update_"+ askyayaColumnId +".xml";
		setPath(filePath);
		setSomething(StringOperation.getDisplayString(strBuf.toString()));
		writeSomething();
	}	
	
	//写xml导库数据(根据数据量来生成数据文件)_列表页处理而来，主要是为了更新价格
	public void writeListXml(String webName,StringBuffer strBuf,String askyayaColumnId,String fileNumber) {

		String filePath = FileRes.getPropValue("listurl.filePath");
		filePath += webName;
		filePath +=File.separator + "update_"+ askyayaColumnId +"_"+ fileNumber+".xml";
		setPath(filePath);
		setSomething(StringOperation.getDisplayString(strBuf.toString()));
		writeSomething();
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}
	
	
	
}

